package com.mmz.webservice;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class WeatherWS {

	/**
	 * 
	 * @throws Exception 
	 */
	public void get(String theCityCode,String theUserID) throws Exception{
		//创建url
		URL url = new URL("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather?" +
				"theCityCode="+theCityCode+"&theUserID="+theUserID);
		//通过url得到http连接
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		//设置http请求相关信息参数
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("GET");
		
		if(conn.getResponseCode()==HttpsURLConnection.HTTP_OK){//结果码=200
			//读取请求返回的数据
			InputStream inputStream = conn.getInputStream();
			byte[] buffer = new byte[1024];
			int len = -1;
			//内存流
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
			while((len=inputStream.read(buffer))!=-1){
				byteArrayOutputStream.write(buffer, 0, len);
			}
			System.out.println("get请求获取的数据:"+byteArrayOutputStream.toString());
			byteArrayOutputStream.close();
			inputStream.close();
		}
	}
	
	public static void main(String[] args) throws Exception {
		WeatherWS mobileCodeWS = new WeatherWS();
		mobileCodeWS.get("北京", "");
	}

}
